package com.coder.bike.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.bike.entity.BikeInfo;
import com.coder.bike.service.IBikeInfoService;
import com.coder.bike.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 单车信息 前端控制器
 * </p>
 *
 * @author 沈诩宸
 * @since 2025-04-22
 */
@RestController
@RequestMapping("/bike/info")
public class BikeInfoController {


    @Resource
    private IBikeInfoService bikeInfoService;

    @PostMapping("{start}/{size}")
    public Result search(@PathVariable int start, @PathVariable int size,
                         @RequestBody BikeInfo bikeInfo) {
        Page<BikeInfo> page = new Page<>(start, size);
        return Result.success(bikeInfoService.searchByPage(page, bikeInfo));
    }

    @PostMapping
    public Result save(@RequestBody BikeInfo bikeInfo) {
        return bikeInfoService.save(bikeInfo) ?
                Result.success() : Result.fail();
    }

    @PutMapping
    public Result update(@RequestBody BikeInfo bikeInfo) {
        return bikeInfoService.updateById(bikeInfo) ?
                Result.success() : Result.fail();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        return Result.success(bikeInfoService.delete(ids));
    }

    @GetMapping("/statusCount")
    public Result getStatusCount() {
        return Result.success(bikeInfoService.getStatusCount());
    }

    @GetMapping("/hasBikeNum/{bikeNum}")
    public Result hasBikeNum(@PathVariable String bikeNum) {
        return Result.success(bikeInfoService.lambdaQuery().eq(BikeInfo::getBikeNum, bikeNum).count() > 0);
    }

    @GetMapping("/list")
    public Result getBikeList() {
        return Result.success(bikeInfoService.list());
    }

}